package com.example.fengyungo_user.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fengyungo_user.R;
import com.example.fengyungo_user.activity.me.DriverDetailActivity;
import com.example.fengyungo_user.activity.me.MyOrderDetailActivity;
import com.example.fengyungo_user.constant.DriverConstant;
import com.example.fengyungo_user.constant.OrderConstant;
import com.example.fengyungo_user.domain.Driver;
import com.example.fengyungo_user.domain.DriverWithNumber;

import java.util.List;

/**
 * @author 徐
 */
public class SelectDriverAdapter extends RecyclerView.Adapter<SelectDriverAdapter.ViewHolder> {

    private static final String TAG = "SelectDriverAdapter";

    protected List<DriverWithNumber> drivers;

    protected Context mContext;
    
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nickname;
        ImageView avatar;
        View driverView;

        public ViewHolder(View view) {
            super(view);
            driverView = view;
            nickname = view.findViewById(R.id.tv_nickname);
            avatar = view.findViewById(R.id.avatar);
        }
    }

    public SelectDriverAdapter(List<DriverWithNumber> driversList) {
        drivers = driversList;
    }

    /**
     * 用于创建ViewHolder实例
     *
     * @param parent   ViewGroup
     * @param viewType ViewHolder
     * @return int
     */
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (mContext == null) {
            mContext = parent.getContext();
        }

        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_driver, parent, false);
        ViewHolder holder = new ViewHolder(view);

        holder.driverView.setOnClickListener(view1 -> {
            int position = holder.getLayoutPosition();
            DriverWithNumber driver = drivers.get(position);
            Intent intent = new Intent(mContext, DriverDetailActivity.class);
            intent.putExtra(DriverConstant.DRIVER, driver);
            mContext.startActivity(intent);
        });

        return holder;
    }

    /**
     * 对RecyclerView子项的数据赋值
     *
     * @param holder ViewHolder
     * @param position int
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DriverWithNumber driver = drivers.get(position);
        holder.nickname.setText(driver.getNickname());
        // TODO: 2024/2/12 头像设置
    }

    @Override
    public int getItemCount() {
        return drivers.size();
    }
}

